import { cloneDeep } from 'lodash';
import { RouteRecordRawMy } from 'vue-router';
const menuRoutes: RouteRecordRawMy[] = [
    // 工作台
    {
        path: '',
        name: 'workplace-layout',
        redirect: { path: '/workplace' },
        component: () => import('../layout/Layout.vue'),
        meta: { title: 'menu.workplace', icon: null, hidden: true },
        children: [
            {
                path: 'workplace',
                name: 'workplace',
                component: () => import('../views/workplace/workplace.vue'),
                meta: { title: 'menu.workplace', icon: 'icon-home', hidden: false }
            }
        ]
    },
    //--------------------分类目录-----仪表盘--------------------
    {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('../layout/Layout.vue'),
        redirect: { name: 'monitor' },
        meta: { title: 'menu.dashboard', icon: 'icon-dashboard', hidden: false },
        children: [
            //监控页
            {
                path: 'monitor',
                name: 'monitor',
                component: () => import('../views/dashboard/monitor/monitor.vue'),
                meta: { title: 'menu.monitor', icon: null, hidden: false },
            },
            //分析页
            {
                path: 'analysis',
                name: 'analysis',
                component: () => import('../views/dashboard/analysis/analysis.vue'),
                meta: { title: 'menu.analysis', icon: null, hidden: false },
            }
        ]
    },
    //--------------------分类目录-----系统管理--------------------
    {
        path: '/system',
        name: 'system',
        component: () => import('../layout/Layout.vue'),
        redirect: { name: 'user' },
        meta: { title: 'menu.system', icon: 'icon-control', hidden: false },
        children: [
            //用户管理
            {
                path: 'user',
                name: 'user',
                component: () => import('../views/system/user/user.vue'),
                meta: { title: 'menu.user', icon: null, hidden: false },
            },
            //角色管理
            {
                path: 'role',
                name: 'role',
                component: () => import('../views/system/role/role.vue'),
                meta: { title: 'menu.role', icon: null, hidden: false },
            }
        ]
    }
];
function getMenuRoutesAll() {
    return cloneDeep(menuRoutes);
}

/**
 * 根据names 获取匹配的菜单路由树
 * @param names 路由name 目录name/叶子name
 * @returns 
 */
function getMenuRoutesByNames(names: string[]) {
    const _menuRoutes = getMenuRoutesAll();
    function setMenuTree(menuList: RouteRecordRawMy[]) {
        const list: RouteRecordRawMy[] = [];
        menuList.forEach(item => {
            //目录/叶子节点有匹配到直接 添加当前树
            if (names.includes(item.name)) {
                list.push(item);
            } else if (item.children) {
                const childrenList = setMenuTree(item.children);
                // children有匹配
                if (childrenList.length) {
                    item.children = childrenList;
                    list.push(item);
                }
            }
        });
        return list;
    };
    return setMenuTree(_menuRoutes);
}

export {
    getMenuRoutesAll,
    getMenuRoutesByNames
};